#!/usr/bin/env python
from lib.format_time import *
from lib.HandleMysql import *
from lib.handle_mongo import *
from lib.switch_code import *
from zhineng_conn import *
from config_tools import *
from imc_conn import *
from lib.load_data import *
import urllib
import json
import time
import math
import sys
import re
reload(sys)
sys.setdefaultencoding('utf-8')

def get_esc_info(date,s58):
    bid_conn = set_bid_conn()
    dict_conn = set_dict_conn()
    ib_conn = set_ib_conn()
    line_list = []
    stat_table='ad_info_data'
    
    for i in range(8):
        query = "select distinct entity_id from creative_info_%s where entity_type=0 and date(add_time)='%s'"%(i,date)
        items = bid_conn.get_results(query)
        if len(items) == 0:
            continue
        
        for item in items:
            info_id = item[0]
            #print info_id
            list_name = ''
            
            params = ''
            info = None
            try:
                info = s58.get_info(info_id)
            except Exception:
                continue
            
            
            #print info
            if info == None:
                continue
            info_cate_id = info.getCateID()
            print "info_cate_id=%s \n" %info_cate_id
            local = info.getLocal()
            print "local id =%s \n" %local
            url = info.getUrl()
            print "url=%s \n" %url
            url = url.replace("http://","")
            list_names = url.strip("/").split("/")
            list_name =  list_names[1]
            print "list_name=%s \n" %list_name
            state = info.getState()
            params = info.getParams()
            print "params=%s \n" %params
            if params == None or params == '':
                continue
            p_list = None
            try:
                params = params.replace("'",'"')
                #print params
                p_list = eval(params)
            except Exception:
                #print params
                continue
           
            #jia ge        
                
    
    bid_conn.close()
    dict_conn.close()
    ib_conn.close()


def execute(date,s58):
    #get_subscribe(date)
    try:
        get_esc_info(date,s58)
    except Exception,e:
        print 'get_esc_info:',e
    
def main():

   #date = '2012-05-28'
   #execute(date)   
   if len(sys.argv)==1:
        start_date=get_yesterday()
        end_date=start_date
   elif len(sys.argv)==2:
        start_date=format_date(sys.argv[1])
        end_date=start_date
   elif len(sys.argv)==3:
        start_date=format_date(sys.argv[1])
        end_date=format_date(sys.argv[2])
   stat_date=start_date
   s58=Service58()
   while stat_date>=start_date and stat_date<=end_date:
        print "The program of the date of %s is beginning at %s " %(stat_date,get_now())
        #
        #get_creative_info(stat_date,s58)
        execute( stat_date,s58) 
        print "The program of the date of %s is end at %s " %(stat_date,get_now())
        stat_date=var_date(stat_date)


if __name__=='__main__':
    main()
